import { Button, ButtonProps, Modal } from "antd";
import { ReactNode, useState } from "react";
export interface ConfirmDeleteModalButtonProps<T = any>
  extends Omit<ButtonProps, "onClick"> {
  service?: () => Promise<T>;
  onSuccess?: () => void;
  modalContent?: ReactNode;
  modalTitle?: string;
}
export default function ConfirmDeleteModalButton<T = any>({
  service,
  onSuccess,
  modalTitle = "删除提示",
  modalContent = "确认删除吗?",
  danger = true,
  ...rest
}: ConfirmDeleteModalButtonProps<T>) {
  const [loading] = useState(false);
  const [disabled, setDisabled] = useState(false);
  return (
    <>
      <Button
        disabled={disabled}
        danger={danger}
        loading={loading}
        onClick={() => {
          setDisabled(true);
          Modal.confirm({
            title: modalTitle,
            content: modalContent,
            onOk: async () => {
              await service?.();
              onSuccess?.();
              setDisabled(false);
            },
            onCancel: () => {
              setDisabled(false);
            },
          });
        }}
        {...rest}
      >
        删除
      </Button>
    </>
  );
}
